
package com.ami.kvm.jviewer.videorecord;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.JVFrame;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.kvm.jviewer.gui.RecorderToolBar;
import com.ami.kvm.jviewer.gui.StandAloneConnectionDialog;
import com.ami.kvm.jviewer.gui.VideoRecord;
import com.ami.kvm.jviewer.gui.WindowFrame;
import com.ami.kvm.jviewer.gui.customizefilechooser;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Timer;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class VideoRecordApp {
	public String Videostorepath;
	public String[] Videotmpstorepath;
	public String[] VideFilename;
	public byte[] writedata;
	public Timer displayTaskTimer;
	public boolean availToConvert;
	public final Object convert;
	public DisplayVideoDataTask displayTask;
	private long duration;
	private JDialog infoDialog;
	private JPanel infoMessagePanel;
	private JLabel infoMessageLabel;
	private URLProcessor downloadurl;
	private String urlPath;
	private int currentFileIndex;

	public VideoRecordApp() {
		this.availToConvert = false;
		this.convert = new Object();

		this.urlPath = null;
	}

	public int startVideorecordRedirection(String paramString1, String paramString2, int paramInt,
			String paramString3) {
		int i = 0;
		int j = 0;
		int k = JViewer.getVideoFile().length;

		this.Videotmpstorepath = new String[k];
		while (j < k) {
			if (JViewer.getVideoFile()[j] != null) {
				showInformationDialog(LocaleStrings.getString("AA_1_VRA"));

				if (JViewer.isRestService() == true) {
					i = restOnconnectHTTPDownloadFile(paramString1, paramString2, paramInt, paramString3, j);
				} else {
					i = OnconnectHTTPDownloadFile(paramString1, paramString2, paramInt, paramString3, j);
				}

				disposeInformationDialog();
				j++;

				if (i < 0) {
					return -1;
				}
			}
		}
		if ((JViewerApp.getInstance().getConnectionDialog() != null)
				&& (JViewerApp.getInstance().getConnectionDialog().isWebLogIn())) {
			JViewerApp.getInstance().getConnectionDialog().logoutWebSession();
		}

		this.currentFileIndex = 0;

		if (JViewer.isdownloadapp()) {
			this.availToConvert = true;
			videoConvertThread localvideoConvertThread = null;
			localvideoConvertThread = new videoConvertThread();
			localvideoConvertThread.start();
		} else {
			OnVideorecordStartRedirection();
			JViewerApp.getInstance().setRedirectionStatus(JViewerApp.REDIR_STARTED);
		}
		return 0;
	}

	public class videoConvertThread extends Thread {
		public videoConvertThread() {
		}

		public void run() {
			int i = 0;
			int j = 0;

			while (VideoRecordApp.this.availToConvert) {
				if ((VideoRecord.Record_Processing) || (VideoRecord.Recording_Started)) {
					try {
						synchronized (VideoRecordApp.this.convert) {
							VideoRecordApp.this.convert.wait();
						}
					} catch (Exception localException) {
						Debug.out.println(localException);
					}
					System.gc();
					JViewerApp.getInstance().initilizeJVVideo();
				}

				i = JViewerApp.getInstance().getVideorecordapp().getFileIndex();
				j = JViewerApp.getInstance().getVideorecordapp().getVideotmpstorepath().length;

				if ((i < j) && (JViewerApp.getInstance().getM_videorecord().getM_recordTimer() != null)) {
					JViewerApp.getInstance().getM_videorecord().getM_recordTimer().cancel();
				}
				JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(),
						LocaleStrings.getString("AA_6_VRA") + " : " + VideoRecordApp.this.VideFilename[i],
						LocaleStrings.getString("A_6_GLOBAL"), 1);

				if (JViewer.isdownloadapp()) {
					VideoRecordApp.this.Videostorepath = VideoRecordApp.this.OngetFoldername();
					if (VideoRecordApp.this.Videostorepath == null) {
						return;
					}
				}

				VideoRecordApp.this.OnVideorecordStartRedirection();
				JViewerApp.getInstance().setRedirectionStatus(JViewerApp.REDIR_STARTED);
			}
		}
	}

	public void Ondeletetmpfile() {
		int i = 0;
		int j = JViewer.getVideoFile().length;
		File localFile = null;

		if (DisplayVideoDataTask.getFc_read() != null) {
			try {
				DisplayVideoDataTask.getFc_read().close();
			} catch (IOException localIOException) {
				localIOException.printStackTrace();
			}
		}
		try {
			while (i < j) {
				localFile = new File(this.Videotmpstorepath[i]);
				localFile.delete();
				i++;
			}
		} catch (Exception localException) {
			Debug.out.println(localException);
			return;
		}
	}

	private int restOnconnectHTTPDownloadFile(String paramString1, String paramString2, int paramInt1,
			String paramString3, int paramInt2) {
		int i = -1;
		String str1 = null;
		this.VideFilename = JViewer.getVideoFile();

		this.downloadurl = new URLProcessor(paramString3, paramInt1);

		if (paramInt1 == 0) {
			this.urlPath = ("http://" + paramString1 + ":" + paramString2 + "/api/logs/video-log?file_name="
					+ this.VideFilename[paramInt2]);
		} else if (paramInt1 == 1) {
			this.urlPath = ("https://" + paramString1 + ":" + paramString2 + "/api/logs/video-log?file_name="
					+ this.VideFilename[paramInt2]);
		}

		i = lockVideoFile(true);
		if (i < 0) {
			Debug.out.println("Lock setting failed");
			return -1;
		}

		String str2 = System.getProperty("java.io.tmpdir");
		this.Videotmpstorepath[paramInt2] = (str2 + "video" + System.currentTimeMillis());

		if (paramInt1 == 0) {
			str1 = "http://" + paramString1 + ":" + paramString2 + "/api/logs/video-data?file="
					+ this.VideFilename[paramInt2];
		} else if (paramInt1 == 1) {
			str1 = "https://" + paramString1 + ":" + paramString2 + "/api/logs/video-data?file="
					+ this.VideFilename[paramInt2];
		}

		try {
			i = this.downloadurl.download(str1);
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			Debug.out.println(localIOException);
		}
		if (i < 0) {
			JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(), LocaleStrings.getString("AA_2_VRA"),
					LocaleStrings.getString("A_5_GLOBAL"), 0);

			i = lockVideoFile(false);
			return -1;
		}

		i = lockVideoFile(false);
		if (i < 0) {
			Debug.out.println("Lock Resetting failed");
			return -1;
		}
		return 0;
	}

	private int OnconnectHTTPDownloadFile(String paramString1, String paramString2, int paramInt1, String paramString3,
			int paramInt2) {
		int i = -1;
		this.VideFilename = JViewer.getVideoFile();
		String str1 = null;

		this.downloadurl = new URLProcessor(paramString3, paramInt1);

		if (paramInt1 == 0) {
			this.urlPath = ("http://" + paramString1 + ":" + paramString2 + "/rpc/downloadvideo.asp?FILE_NAME="
					+ this.VideFilename[paramInt2]);
		} else if (paramInt1 == 1) {
			this.urlPath = ("https://" + paramString1 + ":" + paramString2 + "/rpc/downloadvideo.asp?FILE_NAME="
					+ this.VideFilename[paramInt2]);
		}

		i = lockVideoFile(true);

		if (i < 0) {
			return -1;
		}

		String str2 = System.getProperty("java.io.tmpdir");
		this.Videotmpstorepath[paramInt2] = (str2 + "video" + System.currentTimeMillis());

		if (paramInt1 == 0) {
			str1 = "http://" + paramString1 + ":" + paramString2 + "/video/" + this.VideFilename[paramInt2];
		} else if (paramInt1 == 1) {
			str1 = "https://" + paramString1 + ":" + paramString2 + "/video/" + this.VideFilename[paramInt2];
		}
		try {
			i = this.downloadurl.download(str1);
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
		}

		if (i < 0) {
			JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(), LocaleStrings.getString("AA_2_VRA"),
					LocaleStrings.getString("A_5_GLOBAL"), 0);

			i = lockVideoFile(false);
			return -1;
		}

		i = lockVideoFile(false);

		if (i < 0) {
			return -1;
		}

		return 0;
	}

	public String OngetFoldername() {
		JVFrame localJVFrame = JViewerApp.getInstance().getMainWindow();
		String str = null;
		int i = 1;

		JFileChooser localJFileChooser = new JFileChooser();
		localJFileChooser.setFileSelectionMode(1);
		customizefilechooser localcustomizefilechooser = new customizefilechooser();
		localcustomizefilechooser.customizeFileChooser(localJFileChooser);
		for (;;) {
			i = localJFileChooser.showDialog(localJVFrame, LocaleStrings.getString("V_8_VRS"));

			if (i == 0) {
				break;
			}
			int j = JOptionPane.showConfirmDialog(localJVFrame, LocaleStrings.getString("AA_4_VRA"),
					LocaleStrings.getString("AA_5_VRA"), 0, 3);

			if (j == 0) {
				str = null;
				JViewerApp.getInstance().getM_wndFrame().windowClosed();
			}
		}

		str = localJFileChooser.getSelectedFile().toString();
		return str;
	}

	public void OnVideorecordStartRedirection() {
		if (JViewer.isdownloadapp()) {
			JViewerApp.getInstance().setM_videorecord(new VideoRecord());
			VideoRecord localVideoRecord = JViewerApp.getInstance().getM_videorecord();
			localVideoRecord.StoreLocation = JViewerApp.getInstance().getVideorecordapp().Videostorepath;
			localVideoRecord.OnVideoRecordStart();
		} else {
			RecorderToolBar localRecorderToolBar = (RecorderToolBar) JViewerApp.getInstance().getM_wndFrame()
					.getToolbar();

			localRecorderToolBar.replayButton
					.setIcon(new javax.swing.ImageIcon(JViewer.class.getResource("res/pause.png")));
			localRecorderToolBar.replayButton.setToolTipText(LocaleStrings.getString("R_2_RT"));
			localRecorderToolBar.replayButton.setActionCommand("PAUSE");
		}

		this.displayTask = new DisplayVideoDataTask(JViewerApp.getInstance().getVideorecordapp().getWritedata());

		DisplayVideoDataTask.setCounter(0);
		this.displayTaskTimer = new Timer();
		this.displayTaskTimer.schedule(this.displayTask, 0L);
	}

	public int writedatatofile(String paramString) {
		try {
			File localFile = new File(paramString);
			FileOutputStream localFileOutputStream = new FileOutputStream(localFile);

			if (this.writedata != null) {
				try {
					localFileOutputStream.write(this.writedata);
				} catch (IOException localIOException1) {
					Debug.out.println(localIOException1);
					localFileOutputStream.close();
					return -1;
				}
			}

			try {
				localFileOutputStream.close();
			} catch (IOException localIOException2) {
				return -1;
			}

		} catch (FileNotFoundException localFileNotFoundException) {
			Debug.out.println("FileNotFoundException : " + localFileNotFoundException);
			return -1;
		} catch (Exception localException) {
			Debug.out.println(localException);
			return -1;
		}

		return 0;
	}

	public void showInformationDialog(String paramString) {
		JFrame localJFrame = JViewer.getMainFrame();
		this.infoDialog = new JDialog(localJFrame, "", false);
		this.infoMessagePanel = new JPanel();
		this.infoMessageLabel = new JLabel(paramString);
		this.infoMessageLabel.setHorizontalAlignment(0);
		this.infoMessageLabel.setFont(new Font("Arial", 1, 18));
		this.infoMessagePanel.setLayout(new java.awt.BorderLayout());
		this.infoMessagePanel.add(this.infoMessageLabel, "Center");
		this.infoDialog.setSize(700, 50);
		this.infoDialog.setLocationRelativeTo(localJFrame);
		this.infoDialog.add(this.infoMessagePanel);
		this.infoDialog.setUndecorated(true);
		this.infoDialog.setResizable(false);
		this.infoDialog.setVisible(true);
		this.infoDialog.requestFocus();
		this.infoMessageLabel.setText(paramString);
	}

	public byte[] getWritedata() {
		return this.writedata;
	}

	public void setWritedata(byte[] paramArrayOfByte) {
		this.writedata = paramArrayOfByte;
	}

	public void disposeInformationDialog() {
		this.infoDialog.dispose();
	}

	public void setDuration(long paramLong) {
		this.duration = paramLong;
	}

	public long getDuration() {
		return this.duration;
	}

	public String[] getVideotmpstorepath() {
		return this.Videotmpstorepath;
	}

	public void setVideotmpstorepath(String[] paramArrayOfString) {
		this.Videotmpstorepath = paramArrayOfString;
	}

	public int lockVideoFile(boolean paramBoolean) {
		if (JViewer.isRestService()) {
			return restLockVideoFile(paramBoolean);
		}
		String str = this.urlPath;
		if (paramBoolean) {
			str = str + "&FILE_ACCESS=1";
		} else
			str = str + "&FILE_ACCESS=0";
		return this.downloadurl.processRequest(str);
	}

	public int restLockVideoFile(boolean paramBoolean) {
		String str = this.urlPath;
		if (paramBoolean) {
			str = str + "&file_access=1";
		} else
			str = str + "&file_access=0";
		return this.downloadurl.restProcessRequest(str);
	}

	public int getFileIndex() {
		return this.currentFileIndex;
	}

	public void setFileIndex(int paramInt) {
		this.currentFileIndex = paramInt;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\videorecord\
 * VideoRecordApp.class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */